﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Security.Permissions;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Web.Security;

namespace MRPWeb.MRPProviders
{
    /// <summary>
    /// 自定义的网站导航提供者
    /// </summary>
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class MRPSiteMapProvider : StaticSiteMapProvider
    {
        private Database sitemapDatabase = null;
        private SiteMapNode rootNode = null;
        private string sitemapGetView="";

        // This string is case sensitive.
        private string ConnectionStringName = "SiteMapConnectionString";

        // Implement a default constructor.
        public MRPSiteMapProvider() { }

        // Some basic state to help track the initialization state of the provider.
        private bool initialized = false;
        public virtual bool IsInitialized
        {
            get
            {
                return initialized;
            }
        }
        // Return the root node of the current site map.
        public override SiteMapNode RootNode
        {
            get
            {
                SiteMapNode temp = null;
                temp = BuildSiteMap();
                return temp;
            }
        }

        /// <summary>
        /// 获取本SiteMapProvider管理范围内的根节点
        /// </summary>
        /// <returns></returns>
        protected override SiteMapNode GetRootNodeCore()
        {
            return RootNode;
        }

        // Initialize is used to initialize the properties and any state that the
        // AccessProvider holds, but is not used to build the site map.
        // The site map is built when the BuildSiteMap method is called.
        public override void Initialize(string name, NameValueCollection attributes)
        {
            if (IsInitialized)
                return;

            base.Initialize(name, attributes);

            // Create and test the connection to the Microsoft Access database.

            // Retrieve the Value of the Access connection string from the
            // attributes NameValueCollection.
            string connectionString = attributes[ConnectionStringName];
            sitemapGetView=attributes["GetView"];

            if (null == connectionString || connectionString.Length == 0)
                connectionString = "MRPSqlServer";
            if (null == sitemapGetView || sitemapGetView.Length == 0)
                sitemapGetView = "View_GetUserPermission";

            sitemapDatabase = DatabaseFactory.CreateDatabase(connectionString);

            initialized = true;
        }

        ///
        /// SiteMapProvider and StaticSiteMapProvider methods that this derived class must override.
        ///
        // Clean up any collections or other state that an instance of this may hold.
        protected override void Clear()
        {
            lock (this)
            {
                rootNode = null;
                base.Clear();
            }
        }

        // Build an in-memory representation from persistent
        // storage, and return the root node of the site map.
        public override SiteMapNode BuildSiteMap()
        {

            // Since the SiteMap class is static, make sure that it is
            // not modified while the site map is built.
            lock (this)
            {

                // If there is no initialization, this method is being
                // called out of order.
                if (!IsInitialized)
                {
                    throw new Exception("BuildSiteMap called incorrectly.");
                }

                // If there is no root node, then there is no site map.
                if (null == rootNode)
                {
                    // Start with a clean slate
                    Clear();

                    // Select the root node of the site map from Microsoft Access.
                    int rootNodeId = -1;
                    string whereUserName = "";
                    if (Membership.GetUser() != null)
                        whereUserName = string.Format("where UserName={0}", Membership.GetUser().UserName);
                    string sSQL = string.Format("select [ID],[ModuleName],[Url],[Parent],[IsVisiable],[UserId] from {0} {1} order by [Parent]", sitemapGetView, whereUserName);
                    using (IDataReader reader = sitemapDatabase.ExecuteReader(CommandType.Text, sSQL))
                    {
                        if (reader.Read())
                        {

                        }
                    }

                   
                   
                return rootNode;
            }
        }

    }
}
